package cn.com.cetccst.tools.phone;

import static cn.com.cetccst.tools.phone.Phone.ImsiProvider.PROVIDER_ID_CHINA_MOBILE1;
import static cn.com.cetccst.tools.phone.Phone.ImsiProvider.PROVIDER_ID_CHINA_MOBILE2;
import static cn.com.cetccst.tools.phone.Phone.ImsiProvider.PROVIDER_ID_CHINA_TELECOM;
import static cn.com.cetccst.tools.phone.Phone.ImsiProvider.PROVIDER_ID_CHINA_UNICOM1;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.permissionx.guolindev.PermissionX;

import java.util.List;

import cn.com.cetccst.tools.R;
import cn.com.cetccst.tools.log.CommonLogger;

public class Phone {
    
    interface ImsiProvider {
       String PROVIDER_ID_CHINA_MOBILE1 = "46000";
       String PROVIDER_ID_CHINA_MOBILE2 = "46002";
       String PROVIDER_ID_CHINA_MOBILE3 = "46004";
       String PROVIDER_ID_CHINA_MOBILE4 = "46007";
       String PROVIDER_ID_CHINA_MOBILE5 = "46008";
       String PROVIDER_ID_CHINA_UNICOM1 = "46001";
       String PROVIDER_ID_CHINA_UNICOM2 = "46006";
       String PROVIDER_ID_CHINA_UNICOM3 = "46009";
       String PROVIDER_ID_CHINA_TELECOM = "46003";
    }

    /**
     * 调用该接口需要提前申请权限
     * @param context
     * @return
     */
    @SuppressLint("HardwareIds")
    public static String getProviderName(Context context) {
        String providersName = context.getString(R.string.value_unknown);
        if (PermissionX.isGranted(context, Manifest.permission.READ_PHONE_STATE)) {
            String imsi = null;
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null) {
                try {
                    imsi = telephonyManager.getSubscriberId();
                } catch (Exception e) {
                    CommonLogger.error("can not get device imsi", e);
                }

            }
            if (!TextUtils.isEmpty(imsi)) {
                if (imsi.startsWith(PROVIDER_ID_CHINA_MOBILE1) || imsi.startsWith(PROVIDER_ID_CHINA_MOBILE2)) {
                    providersName = context.getString(R.string.provider_china_mobile);
                } else if (imsi.startsWith(PROVIDER_ID_CHINA_UNICOM1)) {
                    providersName = context.getString(R.string.provider_china_unicom);
                } else if (imsi.startsWith(PROVIDER_ID_CHINA_TELECOM)) {
                    providersName = context.getString(R.string.provider_china_telecom);
                }
            }
        }
        return providersName;
    }

    /**
     * 调用该接口需要提前申请权限
     * @param context
     * @return
     */
    @SuppressLint("HardwareIds")
    public static String getPhone1Number(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (PermissionX.isGranted(context, Manifest.permission.READ_PHONE_STATE)) {
            return telephonyManager != null ? telephonyManager.getLine1Number() : null;
        }
        return "";
    }

    @SuppressLint("HardwareIds")
    public static String getIccId(Context context) {
        if (PermissionX.isGranted(context, Manifest.permission.READ_PHONE_STATE)) {
            SubscriptionManager sm = SubscriptionManager.from(context);
            List<SubscriptionInfo> sis = sm.getActiveSubscriptionInfoList();
            if (sis != null && !sis.isEmpty()) {
                SubscriptionInfo si = sis.get(0);
                return si.getIccId();
            }
        }
        return "";
    }

    public static boolean isRoaming(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager != null && telephonyManager.isNetworkRoaming();
    }
}
